<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../../../assets/docs.css" rel="stylesheet" type="text/css" media="screen" />
<title>MooTools 1.3 参考文档 - Types/String</title>
</head>

<body>
  <div id="docs" class="doc">
    <div class="span-2 last methods">
      <h4 class="menu-item"><a href="#String">String</a></h4>
      <div class="menu-item"><a href="#String:String-from">String.from</a></div>
      <div class="menu-item"><a href="#String:String-uniqueID">String.uniqueID</a></div>
      <div class="menu-item"><a href="#String:test">test</a></div>
      <div class="menu-item"><a href="#String:contains">contains</a></div>
      <div class="menu-item"><a href="#String:trim">trim</a></div>
      <div class="menu-item"><a href="#String:clean">clean</a></div>
      <div class="menu-item"><a href="#String:camelCase">camelCase</a></div>
      <div class="menu-item"><a href="#String:hyphenate">hyphenate</a></div>
      <div class="menu-item"><a href="#String:capitalize">capitalize</a></div>
      <div class="menu-item"><a href="#String:escapeRegExp">escapeRegExp</a></div>
      <div class="menu-item"><a href="#String:toInt">toInt</a></div>
      <div class="menu-item"><a href="#String:toFloat">toFloat</a></div>
      <div class="menu-item"><a href="#String:hexToRgb">hexToRgb</a></div>
      <div class="menu-item"><a href="#String:rgbToHex">rgbToHex</a></div>
      <div class="menu-item"><a href="#String:substitute">substitute</a></div>
      <div class="menu-item"><a href="#String:stripScripts">stripScripts</a></div>
    </div>
    <div class="l-border r-border span-15 content">
      <h1 id="String"><a href="#String">类型: String</a></h1>
      <p class="description">字符串( String )的功能增强</p>
      <h3>另参考:</h3>
      <ul>
        <li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String">MDC String</a></li>
      </ul>
      <h2 id="String:String-from"><a href="#String:String-from">函数: String.from</a></h2>
      <p class="description">将传入参数转换为字符串</p>
      <h3>语法: </h3>
      <pre class="javascript">String.<span class="me1">from</span><span class="br0">(</span>arg<span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>arg - (<em>mixed</em>) 要转为字符串的参数</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 参数转为的字符串</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript">String.<span class="me1">from</span><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span>; <span class="co1">// 返回 '2'</span>
String.<span class="me1">from</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// 返回 'true'</span></pre>
      <h2 id="String:String-uniqueID"><a href="#String:String-uniqueID">函数: String.uniqueID</a></h2>
      <p class="description">产生一个唯一的ID</p>
      <h3>语法: </h3>
      <pre class="javascript">String.<span class="me1">uniqueID</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 一个唯一ID</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript">String.<span class="me1">uniqueID</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h2 id="String:test"><a href="#String:test">String 方法:  test</a></h2>
      <p class="description">使用正则表达式对字符串进行匹配测试. 详情请另参考 <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp/test">MDC Regexp:test</a> . </p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">test</span><span class="br0">(</span>regex<span class="br0">[</span>, params<span class="br0">]</span><span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>regex  - (<em>mixed</em>) 要匹配的字符串或正则表达式</li>
        <li>params - (<em>string</em>, 可选) 如果第一个参数regex是一个字符串, 则本参数可为任何正则表达式参数(注意, 参数'g'无效)</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>boolean</em>) 匹配成功, 返回 <code>true</code></li>
        <li>(<em>boolean</em>) 匹配失败, 返回 <code>false</code></li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'I like cookies'</span>.<span class="me1">test</span><span class="br0">(</span><span class="st0">'cookie'</span><span class="br0">)</span>; <span class="co1">// 返回 true</span>
<span class="st0">'I like cookies'</span>.<span class="me1">test</span><span class="br0">(</span><span class="st0">'COOKIE'</span>, <span class="st0">"i"</span><span class="br0">)</span>; <span class="co1">// 返回 true (ignore case)</span>
<span class="st0">'I like cookies'</span>.<span class="me1">test</span><span class="br0">(</span><span class="st0">'cake'</span><span class="br0">)</span>; <span class="co1">// 返回 false</span></pre>
      <h3>另参考:</h3>
      <ul>
        <li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions">MDC Regular Expressions</a></li>
      </ul>
      <h2 id="String:contains"><a href="#String:contains">String 方法:  contains</a></h2>
      <p class="description">检测字符串内是否包含参数所给出的字符串. <br/>如果给出了separator(分隔符)这个参数, 则匹配时将把主字符串看作按分割符切分的一个列表, 然后将需要检测的字符串和这些列表项进行比较</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">contains</span><span class="br0">(</span>string<span class="br0">[</span>, separator<span class="br0">]</span><span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>string    - (<em>string</em>) 目标测试字符串</li>
        <li>separator - (<em>string</em>, 可选) 分割字符串(如: ' ', ','等).</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>boolean</em>) 如果目标字符串在本字符串内, 则返回 <code>true</code> </li>
        <li>(<em>boolean</em>) 如果目标字符串在本字符串内不存在, 则返回 <code>false</code></li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'a bc'</span>.<span class="me1">contains</span><span class="br0">(</span><span class="st0">'bc'</span><span class="br0">)</span>; <span class="co1">// 返回 true</span>
<span class="st0">'a b c'</span>.<span class="me1">contains</span><span class="br0">(</span><span class="st0">'c'</span>, <span class="st0">' '</span><span class="br0">)</span>; <span class="co1">// 返回 true</span>
<span class="st0">'a bc'</span>.<span class="me1">contains</span><span class="br0">(</span><span class="st0">'b'</span>, <span class="st0">' '</span><span class="br0">)</span>; <span class="co1">// 返回 false</span></pre>
      <h2 id="String:trim"><a href="#String:trim">String 方法:  trim</a></h2>
      <p class="description">清除字符串两端的空白字符串</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">trim</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 清理后的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'    i like cookies     '</span>.<span class="me1">trim</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'i like cookies'</span></pre>
      <h2 id="String:clean"><a href="#String:clean">String 方法:  clean</a></h2>
      <p class="description">清除整个字符串中多余的空白字符串 (<a href="#String:trim">String:trim</a>).</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">clean</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 清理后的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">' i      like     cookies      <span class="es0">\n</span><span class="es0">\n</span>'</span>.<span class="me1">clean</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'i like cookies'</span></pre>
      <h2 id="String:camelCase"><a href="#String:camelCase">String 方法:  camelCase</a></h2>
      <p class="description">将以连字符分隔的字符串转换成以大小写形式进行分隔的字符串.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">camelCase</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 以大小写形式（骆驼式）进行分隔的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'I-like-cookies'</span>.<span class="me1">camelCase</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'ILikeCookies'</span></pre>
      <h2 id="String:hyphenate"><a href="#String:hyphenate">String 方法:  hyphenate</a></h2>
      <p class="description">将以大小写形式进行分隔的字符串转换成以字符分隔的字符串.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">hyphenate</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 以连接字符号分隔的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'ILikeCookies'</span>.<span class="me1">hyphenate</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'I-like-cookies'</span></pre>
      <h2 id="String:capitalize"><a href="#String:capitalize">String 方法:  capitalize</a></h2>
      <p class="description">将字符串中每个单词的首字母大写.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">capitalize</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 转换后的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'i like cookies'</span>.<span class="me1">capitalize</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'I Like Cookies'</span></pre>
      <h2 id="String:escapeRegExp"><a href="#String:escapeRegExp">String 方法:  escapeRegExp</a></h2>
      <p class="description">将字符串中对正则表达式敏感的字符进行转义.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">escapeRegExp</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 转义后的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'animals.sheep[1]'</span>.<span class="me1">escapeRegExp</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'animals\.sheep\[1\]'</span></pre>
      <h2 id="String:toInt"><a href="#String:toInt">String 方法:  toInt</a></h2>
      <p class="description">将字符串解析为一个数值, 并以给出的基准进制计算为一个十进制整数.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">toInt</span><span class="br0">(</span><span class="br0">[</span>base<span class="br0">]</span><span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>base - (<em>number</em>, 可选) 基准进制(默认为10).</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>number</em>) 解析并计算后的数值.</li>
        <li>(<em>NaN</em>) 如果字符串内容不是数值, 则返回 NaN .</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'4em'</span>.<span class="me1">toInt</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 4</span>
<span class="st0">'10px'</span>.<span class="me1">toInt</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 10</span></pre>
      <h3>另参考:</h3>
      <ul>
        <li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt">MDC parseInt</a></li>
      </ul>
      <h2 id="String:toFloat"><a href="#String:toFloat">String 方法:  toFloat</a></h2>
      <p class="description">将字符串解析为一个浮点数.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">toFloat</span><span class="br0">(</span><span class="br0">)</span>;</pre>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>number</em>) 浮点数.</li>
        <li>(<em>NaN</em>) 如果字符串内容不是数值, 则返回 NaN .</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript">    <span class="st0">'95.25%'</span>.<span class="me1">toFloat</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 95.25</span>
    <span class="st0">'10.848'</span>.<span class="me1">toFloat</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 10.848</span></pre>
      <h3>另参考:</h3>
      <ul>
        <li><a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseFloat">MDC parseFloat</a></li>
      </ul>
      <h2 id="String:hexToRgb"><a href="#String:hexToRgb">String 方法:  hexToRgb</a></h2>
      <p class="description">将代表十六进制颜色代码的字符串转换成RGB格式的颜色代码. <br/>字符串的颜色格式必须是如下格式的(可以不带前缀的#):'#ffffff', #fff', 'ffffff', 或 'fff'.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="br0">[</span>array<span class="br0">]</span><span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>array - (<em>boolean</em>, 可选) 如果为true, 则输出为一个数组形式, 而不是通常的字符串形式.</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) RGB格式的颜色代码字符串.</li>
        <li>(<em>array</em>) 如果设置了array为true, 则返回数组形式的RGB颜色代码.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'#123'</span>.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'rgb(17, 34, 51)'</span>
<span class="st0">'112233'</span>.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'rgb(17, 34, 51)'</span>
<span class="st0">'#112233'</span>.<span class="me1">hexToRgb</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// 返回 [17, 34, 51]</span></pre>
      <h2 id="String:rgbToHex"><a href="#String:rgbToHex">String 方法:  rgbToHex</a></h2>
      <p class="description">将代表RGB格式的颜色代码的字符串转换成十六进制颜色代码. <br/>字符串的RGB颜色格式必须是如下格式:"rgb(255,255,255)", 或 "rgba(255,255,255,1)"</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="br0">[</span>array<span class="br0">]</span><span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>array - (<em>boolean</em>, 可选) 如果为true, 则输出为一个数组形式(如： ['ff','33','00']), 而不是通常的字符串形式(如: "#ff3300")</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) 十六进制的颜色代码字符串; 或者, 如果RGB代码设置了第四个值(alpha)为0, 则返回'transparent'</li>
        <li>(<em>array</em>) 如果设置了array为true, 则返回数组形式的十六进制颜色代码</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="st0">'rgb(17, 34, 51)'</span>.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 '#112233'</span>
<span class="st0">'rgb(17, 34, 51)'</span>.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// 返回 ['11', '22', '33']</span>
<span class="st0">'rgba(17, 34, 51, 0)'</span>.<span class="me1">rgbToHex</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'transparent'</span></pre>
      <h3>另参考:</h3>
      <ul>
        <li><a href="../../../docs/core/Types/Array.html#Array:rgbToHex">Array:rgbToHex</a></li>
      </ul>
      <h2 id="String:substitute"><a href="#String:substitute">String 方法:  substitute</a></h2>
      <p class="description">对字符串中给出的替换标记进行值替换(类似简单模板). 清除未定义的关键字和, 忽略多余的关键字. </p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">substitute</span><span class="br0">(</span>object<span class="br0">[</span>, regexp<span class="br0">]</span><span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>object - (<em>mixed</em>) 用于对字符串进行替换填充的对象.</li>
        <li>regexp - (<em>regexp</em>, 可选) 自定义替换的正则表达式模式(需要带有g参数). 默认值: /\?{([^}]+)}/g (即替换大括号标识的区域).</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) - 替换后的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="kw2">var</span> myString = <span class="st0">'{subject} is {property_1} and {property_2}.'</span>;
<span class="kw2">var</span> myObject = <span class="br0">{</span>subject: <span class="st0">'Jack Bauer'</span>, property_1: <span class="st0">'our lord'</span>, property_2: <span class="st0">'saviour'</span><span class="br0">}</span>;
myString.<span class="me1">substitute</span><span class="br0">(</span>myObject<span class="br0">)</span>; <span class="co1">// 返回 'Jack Bauer is our lord and saviour'</span></pre>
      <h2 id="String:stripScripts"><a href="#String:stripScripts">String 方法:  stripScripts</a></h2>
      <p class="description">剥离字符串中 <em><script></script></em> 标签包围的部分.</p>
      <h3>语法: </h3>
      <pre class="javascript">myString.<span class="me1">stripScripts</span><span class="br0">(</span><span class="br0">[</span>evaluate<span class="br0">]</span><span class="br0">)</span>;</pre>
      <h3>参数: </h3>
      <ol>
        <li>evaluate - (<em>boolean</em>, 可选) 如果设置了 evaluate 为 true, 字符串中的脚本将会被执行.</li>
      </ol>
      <h3>返回值: </h3>
      <ul>
        <li>(<em>string</em>) - 剥离了脚本的字符串.</li>
      </ul>
      <h3>示例: </h3>
      <pre class="javascript"><span class="kw2">var</span> myString = <span class="st0">"&lt;script&gt;alert('Hello')&lt;/script&gt;Hello, World."</span>;
myString.<span class="me1">stripScripts</span><span class="br0">(</span><span class="br0">)</span>; <span class="co1">// 返回 'Hello, World.'</span>
myString.<span class="me1">stripScripts</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span>; <span class="co1">// 显示 'Hello', 然后返回 'Hello, World.'</span></pre>
    </div>
  </div>
<script type="text/javascript" src="../../../assets/mootools-core-1.3.js"></script>
<script type="text/javascript" src="../../../assets/mootools-more-smoothscroll.js"></script>
<script type="text/javascript" src="../../../assets/doc-assist.js"></script>
</body>
</html>
